package main

import (
	"fmt"
	"strconv"
	"strings"
)

type Account struct {
	InOrOut string
	Balance float64
	InOrOutMoney float64	
	Explain string
}

type SaveDetail []Account

func Myinit() (int,bool){
	var in string
	fmt.Println("--------------------家庭收支记账软件-----------------------")
	fmt.Println()
	fmt.Println("                      1 收支明细                          ")
	fmt.Println("                      2 登记收入                          ")
	fmt.Println("                      3 登记支出                          ")
	fmt.Println("                      4 退    出                          ")
	fmt.Println()
	fmt.Print("                   请输入选项（1-4）：")
	fmt.Scanln(&in)
	command,err := strconv.Atoi(in)
	if err != nil || command<=0 ||command>4{
		fmt.Println("请输入正确的数字！")
		return 0,false
	}else {
		return command,true
	}
		
}

func (save *SaveDetail) showDetail() {
	fmt.Println("--------------------家庭收支记账软件-----------------------")
	fmt.Println("收支\t账户金额\t收支金额\t说明")
	for i:=0;i<len(*save);i++ {
		fmt.Printf("%v\t  %v\t\t%v\t\t%v\n",(*save)[i].InOrOut,(*save)[i].Balance,
		(*save)[i].InOrOutMoney,(*save)[i].Explain)
	}
}

func (save *SaveDetail) signInput() {
	fmt.Println("--------------------家庭收支记账软件-----------------------")
	var inputMoney float64
	var illustrate string
	fmt.Print("本次收入金额：")
	fmt.Scanln(&inputMoney)
	fmt.Print("本次收入说明：")
	fmt.Scanln(&illustrate)
	var lastBalance float64
	if len(*save) == 0{
		lastBalance = 0
	}else {
		lastBalance = (*save)[len(*save)-1].Balance
	}
	newAccount := Account {
		InOrOut :      "收入",
		Balance :      lastBalance+inputMoney,
		InOrOutMoney : inputMoney,
		Explain :      illustrate,
	}
	*save = append(*save,newAccount)
}

func (save *SaveDetail) signOutput() {
	fmt.Println("--------------------家庭收支记账软件-----------------------")
	var outputMoney float64
	var illustrate string
	fmt.Print("本次支出金额：")
	fmt.Scanln(&outputMoney)
	fmt.Print("本次支出说明：")
	fmt.Scanln(&illustrate)
	var lastBalance float64
	if len(*save) == 0{
		lastBalance = 0
	}else {
		lastBalance = (*save)[len(*save)-1].Balance
	}
	if lastBalance < outputMoney {
		fmt.Println("没有那么多钱")
	}else {
		newAccount := Account {
			InOrOut :      "支出",
			Balance :      lastBalance-outputMoney,
			InOrOutMoney : outputMoney,
			Explain :      illustrate,
		}
		*save = append(*save,newAccount)
	}	
}

func exit() bool{
	fmt.Println("--------------------家庭收支记账软件-----------------------")
	fmt.Print("请确认是否退出（Y/N）:")
	for{
		var isExit string	
		fmt.Scanln(&isExit)
		if strings.EqualFold(isExit,"Y") {
			return true
		}else if strings.EqualFold(isExit,"N") {
			return false
		}else{
			fmt.Print("输入错误，请重新输入：")
			continue
		}
	}	
}

func main()  {
	var save SaveDetail
	label1:for {				
		command,flag := Myinit()
		if !flag {
			continue
		}
		switch command {
		case 1:
			save.showDetail()
		case 2:
			save.signInput()
		case 3:
			save.signOutput()
		case 4:
			if ok := exit(); ok{
				break label1
			}
		default:
			fmt.Println("这个怎么过来的，我很好奇",command)
		}
	}
}